﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Bxas.Utility.Files
{
    public class FileOperations
    {
        public static bool WriteBinaryFile(byte[] fs, string path, string fileName, out string error)
        {
            error = "";
            MemoryStream memoryStream = null;
            FileStream fileStream = null;

            try
            {
                memoryStream = new MemoryStream(fs);
                fileStream = new FileStream(path + fileName, FileMode.Create);
                memoryStream.WriteTo(fileStream);
                memoryStream.Close();
                fileStream.Close();
                fileStream = null;
                memoryStream = null;
                return true;
            }
            catch (Exception ex)
            {
                error = ex.Message;
                if (memoryStream != null)
                    memoryStream.Close();
                if (fileStream != null)
                    fileStream.Close();
                fileStream = null;
                memoryStream = null;
                return false;
            }
        }

        public static bool RemoveFile(string url)
        {
            try
            {
                if (System.IO.File.Exists(url))
                    System.IO.File.Delete(url);

                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
